package servlets;

import java.io.IOException;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import careerSession.CareerSession;
import careerSession.CareerSessionHome;

import companySupSession.CompanySupSession;
import companySupSession.CompanySupSessionHome;

import markerSession.MarkerSession;
import markerSession.MarkerSessionHome;
import recruiterSession.RecruiterSession;
import recruiterSession.RecruiterSessionHome;
import studentSession.StudentSession;
import studentSession.StudentSessionHome;
import user.User;
import user.UserHome;
import academicSupSession.AcademicSupSession;
import academicSupSession.AcademicSupSessionHome;
import adminSession.adminSession;
import adminSession.adminSessionHome;
import extras.Md5Encryption;

public class Dispatcher extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException
	{
		doPostAndGet(req,res);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException
	{
		doPostAndGet(req,res);
	}

	protected void doPostAndGet(HttpServletRequest req ,HttpServletResponse res)
	throws IOException, ServletException
	{
		String login = req.getParameter("login");
		String passwd = req.getParameter("passwd");
		UserHome userHome = null;
		MarkerSessionHome markerSessionHome = null;
		StudentSessionHome studentSessionHome = null;
		AcademicSupSessionHome academicSupSessionHome = null;
		RecruiterSessionHome recruiterSessionHome = null;
		CareerSessionHome careerSessionHome = null;
		User user = null ;


		InitialContext initialContext = null;
		try {
			initialContext = new InitialContext();
			userHome = (UserHome)initialContext.lookup("MyUser_L");
		} catch (NamingException e) {
			e.printStackTrace();
		}
		try {
			user = userHome.findByLoginPassword(login,Md5Encryption.getEncodedPassword(passwd));
		} catch (FinderException e) {
			//l'utilisateur n'existe pas
			user = null;
			System.out.print("Wrong user and/or passwd\n");
			//e.printStackTrace();
			Notification.getNotification(Notification.appl_password_wrong,"Dispatcher");
		}
		if (user == null)
		{
			req.setAttribute("error1", Notification.getNotification(Notification.login_password_wrong,""));
			req.getRequestDispatcher("index.jsp").forward(req, res);

		}else{
			Notification.getNotification(Notification.logged,user.getLogin());
			int type = user.getType();
			RequestDispatcher dispatcher = null ;
			switch (type) {
			case 1: //STUDENT
				StudentSession studentSession = null;
				try 
				{
					Object objref = initialContext.lookup("MyStudentSession") ;
					studentSessionHome = (StudentSessionHome)PortableRemoteObject.narrow(objref, StudentSessionHome.class) ;
					studentSession = studentSessionHome.create(user.getId());
				}
				catch (Exception e) {e.printStackTrace();} 
				req.getSession().setAttribute("studentSession",studentSession);
				res.sendRedirect("./StudentInterface");
				break;

			case 2:// ADMIN
				try {
					Object objref = initialContext.lookup("MyAdminSession") ;
					adminSessionHome adminSessionHome = (adminSessionHome)PortableRemoteObject.narrow(objref, adminSessionHome.class) ;
					adminSession adminSession = adminSessionHome.create(user.getId());
					adminSession.login();
					req.getSession().setAttribute("adminSession",adminSession);
				} catch (NamingException e) {
					e.printStackTrace();
				} catch (CreateException e) {
					e.printStackTrace();
				}
				dispatcher = req.getRequestDispatcher("admin");
				break;
			case 3: //COMPANYSUPERVISOR
				try {
					Object objref = initialContext.lookup("MyCompanySupSession") ;
					CompanySupSessionHome companySupSessionHome = (CompanySupSessionHome)PortableRemoteObject.narrow(objref, CompanySupSessionHome.class) ;
					CompanySupSession companySupSession = companySupSessionHome.create();
					int logResult = companySupSession.log(user.getId());
					req.getSession().setAttribute("companySupSession",companySupSession);
				} catch (NamingException e) {
					e.printStackTrace();
				} catch (CreateException e) {
					e.printStackTrace();
				}
				dispatcher = req.getRequestDispatcher("companySup");
				break;
			case 4: //CAREER
				CareerSession careerSession = null;
				try 
				{
					Object objref = initialContext.lookup("MyCareerSession") ;
					careerSessionHome = (CareerSessionHome)PortableRemoteObject.narrow(objref, CareerSessionHome.class) ;
					careerSession = careerSessionHome.create(user.getId());
				}
				catch (Exception e) {e.printStackTrace();} 
				req.getSession().setAttribute("careerSession",careerSession);
				res.sendRedirect("./CareerInterface");
				break;
			case 5: //MARKER
				MarkerSession markerSession = null;
				try {
					Object objref = initialContext.lookup("MyMarkerSession") ;
					markerSessionHome = (MarkerSessionHome)PortableRemoteObject.narrow(objref, MarkerSessionHome.class) ;
					markerSession = markerSessionHome.create(user.getId());
				} catch (NamingException e) {

					e.printStackTrace();
				} catch (CreateException e) {
					e.printStackTrace();
				}
				req.getSession().setAttribute("userSurname",markerSession.getSurname());
				req.getSession().setAttribute("userFirstname", markerSession.getFirstname());
				req.setAttribute("listStudents", markerSession.getStudentListDescriptor());
				dispatcher = req.getRequestDispatcher("marker/markerView.jsp");
				req.getSession().setAttribute("marker", markerSession);
				break;
			case 6: //RECRUITER
				RecruiterSession recruiterSession =null;
				try{
					Object objref = initialContext.lookup("MyRecruiterSession");
					recruiterSessionHome = (RecruiterSessionHome)PortableRemoteObject.narrow(objref, RecruiterSessionHome.class);
					recruiterSession = recruiterSessionHome.create(user.getId());
				}catch (NamingException e) {

					e.printStackTrace();
				} catch (CreateException e) {
					e.printStackTrace();
				}
				req.getSession().setAttribute("userSurname",recruiterSession.getSurname());
				req.getSession().setAttribute("userFirstname", recruiterSession.getFirstname());
				req.getSession().setAttribute("recruiter", recruiterSession);
				req.setAttribute("studentList", recruiterSession.getInterestedStudent());
				req.setAttribute("recruiter", recruiterSession.getDescriptor());
				req.setAttribute("degreeList", recruiterSession.getDegreeDescriptor());
				dispatcher = req.getRequestDispatcher("recruiter/recruiterView.jsp");
				break;
			case 7: //ACADEMIC SUPERVISOR
				AcademicSupSession academicSession = null;
				try {
					Object objref = initialContext.lookup("MyAcademicSupSession") ;
					academicSupSessionHome = (AcademicSupSessionHome)PortableRemoteObject.narrow(objref, AcademicSupSessionHome.class) ;
					academicSession = academicSupSessionHome.create(user.getId());
				} catch (NamingException e) {

					e.printStackTrace();
				} catch (CreateException e) {
					e.printStackTrace();
				}
				req.setAttribute("userSurname",academicSession.getSurname());
				req.setAttribute("userFirstname", academicSession.getFirstname());
				req.getSession().setAttribute("academicSup", academicSession);
				req.setAttribute("listStudents", academicSession.getStudents());
				req.setAttribute("academicSup", academicSession.getDescriptor());
				dispatcher = req.getRequestDispatcher("academicSupervisor/academicSupervisorView.jsp");
				break;

			default:
				break;
			}

			if (dispatcher != null) dispatcher.forward(req, res) ;
		}

	}
}
